/* eslint-disable */

// 引入axios
import axios from 'axios'

export default {
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        // name规定校验区域
        username: [
          // 一个对象就是一条校验规则
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 8 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) {
          return alert('登录失败!')
        }

        let res = await axios.post(
          'http://localhost:8888/api/private/v1/login',
          this.ruleForm
        )
        console.log(res)
        if (res.data.meta.status === 200) {
          // 把登录成功后服务器传过来的token保存在本地
          localStorage.setItem('token', res.data.data.token)

          // elementui 给vue.prototype添加的属性$message，弹窗
          this.$message({
            message: '登录成功',
            type: 'success'
          })

          this.$router.push('/home')
        } else {
          // elementui 给vue.prototype添加的属性$message，弹窗
          this.$message({
            message: res.data.meta.msg,
            type: 'warning'
          })
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    }
  }
}
